<?php

namespace coreapi\controllers;

use common\constants\Constants;
use Yii;
use yii\web\Response;
use yii\web\Controller;

/**
 *@auth jianbiao11
 *
 **/
class BaseSwooleController  extends Controller {

	public $swoole_client;

	public function beforeAction($action) {

		//获取请求的URL
		$route = Yii::$app->requestedRoute;

		$this->swoole_client = \Yii::$app->swoole_client;
		//字模块定义文件，基于此文件可完成服务的单独部署
		$filename = \Yii::getAlias('@coreapi') . "/config/rpcserver/server.conf.json";

		//custom initialization code goes here
		if (file_exists($filename)) {
			//TODO:这里添加从redis读取服务器配置信息
			$configJson = file_get_contents($filename);
			\Yii::info("server.conf.json:$configJson");
			$serverConfig = json_decode($configJson, true);
			$this->swoole_client->initConfig($serverConfig);
			//手动加入模式代码
			$mode1 = array("type" => 1, "group" => "group1");
	        $mode2 = array("type" => 2, "ip" => "182.92.216.33", "port" => 9502);
	        $this->swoole_client->changeMode($mode2);
		} else {
			//暂时抛出异常
			throw new Exception("modules.json not found", 1);
		}

		$this->swoole_client = $this->swoole_client->getClient();
		return parent::beforeAction($action);
	}

	/**
	 * 执行成功，组装应答数据
	 * @param array $data 数据
	 * @param string $msg 提示信息
	 * @param int $status 状态码
	 * @return array
	 */
	protected function getSuccessArray($data = [], $msg = '请求成功', $status = Constants::STATUS_SUCCESS) {
		return \Yii::$app->utils->getSuccessArray($data, $msg, $status);
	}

	/**
	 * 执行失败，组装应答数据
	 * @param string $msg 提示信息
	 * @param int $status 状态码
	 * @param array $data 数据
	 * @return array
	 */
	protected function getFailedArray($msg = '', $status = Constants::STATUS_FAILED, $data = []) {
		return \Yii::$app->utils->getFailedArray($msg, $status, $data);
	}

	/**
	 * 返回json数据
	 * @param $data
	 * @return \yii\console\Response|Response
	 */
	protected function returnJson($data) {
		\Yii::info("coreapi 返回结果为：" . json_encode($data, JSON_UNESCAPED_UNICODE));
		return $this->assembleAppResponse($data);
	}

	/**
	 * 返回json格式SUCESS数据
	 * @param array $data
	 * @param string $msg
	 * @param int $status
	 * @return \yii\console\Response|Response
	 */
	protected function returnSuccessJson($data = [], $msg = '请求成功', $status = Constants::STATUS_SUCCESS) {
		return $this->returnJson($this->getSuccessArray($data, $msg, $status));
	}

	/**
	 * 返回json格式FAILED数据
	 * @param string $msg
	 * @param int $status
	 * @param array $data
	 * @return \yii\console\Response|Response
	 */
	protected function returnFailedJson($msg = '', $status = Constants::STATUS_FAILED, $data = []) {
		return $this->returnJson($this->getFailedArray($msg, $status, $data));
	}

	/**
	 * 组装app端返回数据
	 * @param $params
	 * @return \yii\console\Response|Response
	 */
	private function assembleAppResponse($params) {
		\Yii::$app->response->format = Response::FORMAT_JSON;
		\Yii::$app->response->data = $params;
		return \Yii::$app->response;
	}

	/**
	 * 调试、打印变量
	 * @param mixed $res
	 */
	public function p($res) {
		if (is_string($res)) {
			echo $res . "<br />\n";
		} elseif (is_array($res)) {
			echo "<pre>\n";
			print_r($res);
			echo '</pre>\n';
		} else {
			echo "<pre>\n";
			var_dump($res);
			echo "</pre>\n";
		}
	}

}
